//
//  View.swift
//  BaseModule_Example
//
//  Created by seven on 2020/7/2.
//  Copyright © 2020 CocoaPods. All rights reserved.
//

import UIKit

// AdaptivePerfect
public extension UIView {

    /// 适配约束
    func tt_fitAllConstraint() {
        tt_fitAllConstraint(false)
    }

    /// 适配约束
    /// - Parameter isNeedFitFont: 是否需要适配字体
    func tt_fitAllConstraint(_ isNeedFitFont: Bool) {
        if UIScreen.main.bounds.width == 375.0 {
            return
        }
        _fitAllConstraint(self, isNeedFitFont: isNeedFitFont)
        setNeedsLayout()
    }

    /// 快速创建view
    /// - Parameters:
    ///   - backgroundColor: 背景颜色
    ///   - frame: frame
    /// - Returns: view
    class func with(_ backgroundColor: UIColor, frame: CGRect) -> Self {
        let view = self.init(frame: frame)
        view.backgroundColor = backgroundColor
        return view
    }

    /// 设置各个方向的圆角
    /// - Parameters:
    ///   - cornerRadius: 圆角大小
    ///   - rounding: 方向
    ///   - rect: 范围
    func setCornerRadius(_ cornerRadius: CGFloat, rounding: UIRectCorner, rect: CGRect? = nil) {
        let layer = CAShapeLayer()
        let r: CGRect
        if let rect = rect {
            r = rect
        } else {
            r = bounds
        }
        let path = UIBezierPath(roundedRect: r, byRoundingCorners: rounding, cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
        layer.path = path.cgPath
        self.layer.mask = layer
    }


    /// 设置圆角、边框、边框颜色
    /// - Parameters:
    ///   - cornerRadius: 圆角
    ///   - borderColor: 边框颜色
    ///   - borderWidth: 边框
    func setCornerRadius(_ cornerRadius: CGFloat, borderColor: UIColor, borderWidth: CGFloat) {
        layer.cornerRadius = cornerRadius
        layer.masksToBounds = true
        layer.borderWidth = borderWidth
        layer.borderColor = borderColor.cgColor
    }


    /// 设置边框
    /// - Parameters:
    ///   - color: 颜色
    ///   - width: 宽度
    func setBorder(_ color: UIColor, width: CGFloat) {
        layer.borderWidth = width
        layer.borderColor = color.cgColor
    }

    /// 递归调用适配方法
    /// - Parameters:
    ///   - isNeedFitFont: 是否需要适配字体
    private func _fitAllConstraint(_ view: UIView, isNeedFitFont: Bool) {
        for cons in view.constraints {
            cons.constant = fit(cons.constant)
        }
        if view.subviews.count == 0 {
            return
        }

        if isNeedFitFont { // 适配字体大小
            for subview in view.subviews {
                if subview is UILabel {
                    let label = subview as! UILabel
                    label.font = UIFont.systemFont(ofSize: fit(label.font.pointSize))
                } else if subview is UIButton {
                    let button = subview as! UIButton
                    button.titleLabel?.font = UIFont.systemFont(ofSize: fit(button.titleLabel?.font.pointSize ?? 14))
                } else if subview is UITextField {
                    let textField = subview as! UITextField
                    textField.font = UIFont.systemFont(ofSize: fit(textField.font?.pointSize ?? 14))
                } else if subview is UITextView {
                    let textView = subview as! UITextView
                    textView.font = UIFont.systemFont(ofSize: fit(textView.font?.pointSize ?? 14))
                }

                _fitAllConstraint(subview, isNeedFitFont: true)
            }
        } else {
            for subview in view.subviews {
                _fitAllConstraint(subview, isNeedFitFont: false)
            }
        }
    }

    static var scale: CGFloat {
        UIScreen.main.bounds.width / 375.0
    }

    /// 创建跟类名一样的xib
    class func createViewNib() -> Self {
        let str = NSStringFromClass(self.classForCoder()).components(separatedBy: ".").last!
        let view = Bundle.main.loadNibNamed(str, owner: nil, options: nil)?.last
        return (view as! Self)
    }

    /// 返回当前view所在的控制器
    var viewController: UIViewController? {
        var sv = self.superview
        while sv != nil {
            let next = sv!.next
            if let n = next, n is UIViewController {
                return n as? UIViewController
            }
            sv = sv?.superview
        }
        return nil
    }
}

public extension UIView {

    var tt_cornerRadius: CGFloat {
        set {
            layer.cornerRadius = fit(newValue)
            layer.masksToBounds = true
        }
        get {
            layer.cornerRadius
        }
    }

    var x: CGFloat {
        set {
            guard x != newValue else {
                return
            }
            frame.origin.x = newValue
        }
        get {
            frame.origin.x
        }
    }
    var y: CGFloat {
        set {
            guard y != newValue else {
                return
            }
            frame.origin.y = newValue
        }
        get {
            frame.origin.y
        }
    }
    var width: CGFloat {
        set {
            guard width != newValue else {
                return
            }
            frame.size.width = newValue
        }
        get {
            frame.size.width
        }
    }
    var height: CGFloat {
        set {
            guard height != newValue else {
                return
            }
            frame.size.height = newValue
        }
        get {
            frame.size.height
        }
    }
    var centerX: CGFloat {
        set {
            guard center.x != newValue else {
                return
            }
            center.x = newValue
        }
        get {
            center.x
        }
    }

    var centerY: CGFloat {
        set {
            guard center.y != newValue else {
                return
            }
            center.y = newValue
        }
        get {
            center.y
        }
    }
    var maxX: CGFloat {
        set {
            x = newValue - width
        }
        get {
            x + width
        }
    }
    var maxY: CGFloat {
        set {
            y = newValue - height
        }
        get {
            y + height
        }
    }
    var halfHeight: CGFloat {
        height / 2
    }
    var halfWidth: CGFloat {
        width / 2
    }

    var cornerRadius: CGFloat {
        set {
            layer.cornerRadius = newValue
            layer.masksToBounds = true
        }
        get {
            layer.cornerRadius
        }
    }

    var borderColor: UIColor? {
        set {
            guard let c = newValue else {
                return
            }
            layer.borderColor = c.cgColor
        }
        get {
            guard let c = layer.borderColor else {
                return nil
            }
            return UIColor(cgColor: c)
        }
    }
    var borderWidth: CGFloat {
        set {
            layer.borderWidth = newValue
        }
        get {
            layer.borderWidth
        }
    }
}
